package com.imooc.controller;


import com.imooc.product.client.ProductClient;
import com.imooc.product.common.ProductInfoOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2019/3/13.
 * 测试用
 */
@RestController
@Slf4j
public class ClientController {

    /*@Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;*/

    @Autowired
    private ProductClient productClient;

    @RequestMapping("/getProductMsg")
    public String getProductMsg() {

        //1.第一种方式(使用restTemplate,写死了url,如果部署了多个就不行了)
//        RestTemplate restTemplate = new RestTemplate();
//        String response = restTemplate.getForObject("http://localhost:8080/msg", String.class);

        //2.第二种方法(使用restTemplate,通过应用名获取url)
        /*RestTemplate restTemplate = new RestTemplate();
        ServiceInstance serviceInstance = loadBalancerClient.choose("PRODUCT");
        String url = String.format("http://%s:%s", serviceInstance.getHost(), serviceInstance.getPort() + "/msg");
        String response = restTemplate.getForObject(url, String.class);*/

        //第三种方法(写配置,利用@LoadBalanced,在resttemplate中写应用名)
//        String response = restTemplate.getForObject("http://PRODUCT/msg", String.class);

        //使用feign调用
        String response = productClient.productMsg();


        log.info("response={}",response);

        return response;

    }


    @RequestMapping("/getProducList")
    public String getProducList() {
        List<ProductInfoOutput> productInfoList = productClient.listForOrder(Arrays.asList("3687341987"));

        log.info("productInfoList={}",productInfoList.toString());
        return null;
    }

    @Value("${env}")
    private String env;

    @RequestMapping("/env/getEnv")
    public String getEnv() {
        log.info("env={}",env);
        return env;
    }


}
